#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <string>
#include"Mutex.hpp"
int ticket = 1000; // 共享资源
//pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
using namespace Mutex;
struct ThreadData
{
    ThreadData(const std::string &name, MyMutex*p)
        : _name(name), _lockp(p)
    {
    }
    std::string _name;
    MyMutex *_lockp;
};
void *route(void *arg)
{
    
    //char *id = (char *)arg;
    while (1)
    {
        char *id = (char *)arg;
        ThreadData *td = static_cast<ThreadData*>(arg);
        //pthread_mutex_lock(td->_lockp);
        td->_lockp->Lock();
        /////////////////////////////////////////////////临界区
        if (ticket > 0)
        {
            usleep(1000);
            printf("%s sells ticket:%d\n", td->_name.c_str(), ticket);
            ticket--;
            td->_lockp->UnLock();
            //pthread_mutex_unlock(td->_lockp);
            ////////////////////////////////////////////临界区
        }
        else
        {
            //pthread_mutex_unlock(td->_lockp);
            td->_lockp->UnLock();
            break;
        }
    }
    return nullptr;
}
int main(void)
{
    //pthread_mutex_t lock;
    MyMutex lock;
    pthread_t t1, t2, t3, t4;
    ThreadData *td1 = new ThreadData("thread 1",&lock);
    pthread_create(&t1, NULL, route,td1);
    ThreadData *td2 = new ThreadData("thread 2",&lock);
    pthread_create(&t2, NULL, route,td1);
    ThreadData *td3 = new ThreadData("thread 3",&lock);
    pthread_create(&t3, NULL, route,td1);
    ThreadData *td4 = new ThreadData("thread 4",&lock);
    pthread_create(&t4, NULL, route,td1);


    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);
    pthread_join(t4, NULL);

    //pthread_mutex_destroy(&lock);
    return 0;
}
// struct ThreadData
// {
//     ThreadData(const std::string &name, pthread_mutex_t *p)
//         : _name(name), _lockp(p)
//     {
//     }
//     std::string _name;
//     pthread_mutex_t *_lockp;
// };
// void *route(void *arg)
// {
//     ThreadData *td = static_cast<ThreadData*>(arg);
//     //char *id = (char *)arg;
//     while (1)
//     {
//         char *id = (char *)arg;
//         pthread_mutex_lock(td->_lockp);
//         /////////////////////////////////////////////////临界区
//         if (ticket > 0)
//         {
//             usleep(1000);
//             printf("%s sells ticket:%d\n", td->_name.c_str(), ticket);
//             ticket--;
//             pthread_mutex_unlock(td->_lockp);
//             ////////////////////////////////////////////临界区
//         }
//         else
//         {
//             pthread_mutex_unlock(td->_lockp);
//             break;
//         }
//     }
//     return nullptr;
// }
// int main(void)
// {
//     pthread_mutex_t lock;
//     pthread_t t1, t2, t3, t4;
//     ThreadData *td1 = new ThreadData("thread 1",&lock);
//     pthread_create(&t1, NULL, route,td1);
//     ThreadData *td2 = new ThreadData("thread 2",&lock);
//     pthread_create(&t2, NULL, route,td1);
//     ThreadData *td3 = new ThreadData("thread 3",&lock);
//     pthread_create(&t3, NULL, route,td1);
//     ThreadData *td4 = new ThreadData("thread 4",&lock);
//     pthread_create(&t4, NULL, route,td1);


//     pthread_join(t1, NULL);
//     pthread_join(t2, NULL);
//     pthread_join(t3, NULL);
//     pthread_join(t4, NULL);

//     pthread_mutex_destroy(&lock);
//     return 0;
// }